package com.sessioncookie;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @classDesc: 功能描述:(創建Cookie案例)
 */
@WebServlet("/AddCookieServlet")
public class AddCookieServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		 // key value 自定义Cookie 
		 Cookie cookie = new Cookie("userName", "123456");
		 // 如果是负数浏览器关闭就失效， 如果是正数以秒为单位进行保存。
	      cookie.setMaxAge(60*60*24);// cookie保存一天。。 没有任何一家公司能应该保存登录
		 // 将Cookie 发送客户端.
		 resp.addCookie(cookie);
		 System.out.println("创建Cookie成功!");
	}
	
}
